package com.practice.text;

import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.function.Function;

public class Encode {

	public static void main(String[] args) throws Exception {
		encodeTextToBase64();
	}	
	
	public void encodeByteToDigit() {
		int bin = Integer.parseInt("10", 2); System.out.println(bin);
		int oct = Integer.parseInt("10", 8); System.out.println(oct);
		int dec = Integer.parseInt("10", 10); System.out.println(dec);
		int hex = Integer.parseInt("10", 16); System.out.println(hex);
		
		bin = 10; System.out.println(Integer.toBinaryString(bin));
		oct = 10; System.out.println(Integer.toOctalString(oct));
		dec = 10; System.out.println(Integer.toString(dec));
		hex = 10; System.out.println(Integer.toHexString(hex));
	}
	
	public static void encodeTextToBase64() {
		
		String[] texts = { "any carnal pleasure.", "any carnal pleasure", "any carnal pleasur",
				"any carnal pleasu", "any carnal pleas" };
		
		for (String text : texts) {
			byte[] src = text.getBytes();
			
			System.out.println(text);
			System.out.println(encodeByteToBase64(src));
			System.out.println(encodeByteToDigit(src, Integer::toHexString));
		}
		
	}
	
	private static String encodeByteToBase64(byte[] src) {
		Encoder encoder = Base64.getEncoder();
		return encoder.encodeToString(src);
	}
	
	private static String encodeByteToDigit(byte[] src, 
			Function<Byte, String> digitFunction) {
		StringBuilder stringBuilder = new StringBuilder();
		for (byte b : src) {
			stringBuilder.append(digitFunction.apply(b));
		}
		return stringBuilder.toString();
	}
	
}
